﻿using Dimebrain.TweetSharp.Core.Attributes;

namespace Dimebrain.TweetSharp.Fluent
{
    public static class ITwitterSavedSearchesExtensions
    {
        [RequiresAuthentication]
        public static ITwitterSavedSearchesList List(this ITwitterSavedSearches instance)
        {
            return new TwitterSavedSearchesList(instance.Root);
        }

        [RequiresAuthentication]
        public static ITwitterSavedSearchesCreate Create(this ITwitterSavedSearches instance, string query)
        {
            instance.Root.Parameters.Action = "create";
            instance.Root.SearchParameters.SearchPhrase = query;

            return new TwitterSavedSearchesCreate(instance.Root);
        }

        [RequiresAuthentication]
        public static ITwitterSavedSearchesCreate Create(this ITwitterSavedSearches instance, ITwitterSearchQuery query)
        {
            instance.Root.Parameters.Action = "create";

            // Clean query URL
            var url = query.AsUrl();
            var index = url.IndexOf("?q=%23");
            var value = url.Substring(index + 6);

            instance.Root.SearchParameters.SearchPhrase = value;
            return new TwitterSavedSearchesCreate(instance.Root);
        }

        [RequiresAuthentication]
        public static ITwitterSavedSearchesDestroy Delete(this ITwitterSavedSearches instance, int id)
        {
            instance.Root.Parameters.Action = "destroy";
            instance.Root.Parameters.Id = id;
            return new TwitterSavedSearchesDestroy(instance.Root);
        }

        [RequiresAuthentication]
        public static ITwitterSavedSearchesDestroy Delete(this ITwitterSavedSearches instance, long id)
        {
            instance.Root.Parameters.Action = "destroy";
            instance.Root.Parameters.Id = id;
            return new TwitterSavedSearchesDestroy(instance.Root);
        }

        [RequiresAuthentication]
        public static ITwitterSavedSearchesShow Show(this ITwitterSavedSearches instance, int id)
        {
            instance.Root.Parameters.Action = "show";
            instance.Root.Parameters.UserId = id;
            return new TwitterSavedSearchesShow(instance.Root);
        }

        [RequiresAuthentication]
        public static ITwitterSavedSearchesShow Show(this ITwitterSavedSearches instance, long id)
        {
            instance.Root.Parameters.Action = "show";
            instance.Root.Parameters.UserId = id;
            return new TwitterSavedSearchesShow(instance.Root);
        }
    }
}
